//
//  XXTableView2.swift
//  XXSwiftDemo
//
//  Created by admin on 2022/4/14.
//

import UIKit

public typealias XXTableViewClickBlock = (Int,Int)->Void
public func kIndexPath(_ row:Int, _ section:Int) -> IndexPath{
    IndexPath.init(row: 1, section: 0)
}

class XXBaseTableView: UIView, UITableViewDelegate, UITableViewDataSource {
    enum XXTableViewType{
        case plain, grouped
    }
    var identifier = ""
    var page = 1
    var pageSize = 20
    var rows = 0
    var sections = 1
    var heightForRow = 40.0
    var heightForHeader = 0.0
    var heightForFooter = 0.0
    var cellClass = XXBaseTableViewCell.self
    var type = UITableView.Style.plain
    lazy var tableView:UITableView = {
        let  tableView = UITableView.init(frame: .zero, style: type)
        tableView.delegate = self
        tableView.dataSource = self
        return tableView
    }()
    var clickBlock:XXTableViewClickBlock?
    var isCanClick = true
    var selectTimeInterval = 0.5
    var arrData:Array<XXBaseModel> = []
    
    convenience init(_ style: UITableView.Style){
        self.init(frame: .zero)
        type = style
        addSubview(tableView)
        tableView.snp.makeConstraints { make in
            make.top.left.bottom.right.equalTo(0)
        }
        config()
    }
    // 配置
    func config() {
        fatalError("Must Override")
    }
    
    // MARK: 刷新数据
    func addData(data:Array<XXBaseModel>){
        arrData = arrData.merge(data) as! Array<XXBaseModel>
        reloadData()
    }
    
    // MARK: 刷新
    final func reloadData(){
        tableView.reloadData()
    }
    final func reloadRows(_ arr:Array<IndexPath>){
        tableView.reloadRows(at: arr, with: .fade)
    }
    final func reloadSections(_ sections: IndexSet){
        tableView.reloadSections(sections, with: .fade)
    }
    
    // MARK: 子类重写DataSource
    func numberOfRowsInSection(section: Int) -> Int {
        rows
    }
    func numberOfSections(in tableView: UITableView) -> Int {
        sections
    }
    func heightForRow(row: Int, section:Int) -> CGFloat {
        heightForRow
    }
    // 子类重写
    func contentView<T:UITableViewCell>(forRow row: Int, section:Int, contentView:UIView ,cell:T) { }
    // 加载cell
    func loadCell<T:UITableViewCell>(_ cellClass:T.Type, _ tableView: UITableView,_ indexPath: IndexPath) ->T {
        var cell:T
        if let reusableCell = tableView.dequeueReusableCell(withIdentifier: identifier){
            cell = reusableCell as! T
        }else{
            cell = cellClass.init(style: .default, reuseIdentifier: identifier)
        }
        return cell
    }
    
    // MARK: DataSource
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return numberOfRowsInSection(section: section)
    }
    // cell
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = loadCell(cellClass, tableView, indexPath)
        cell.row = indexPath.row
        cell.section = indexPath.section
        contentView(forRow: indexPath.row, section: indexPath.section, contentView: cell.contentView, cell: cell)
        return cell
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        heightForRow(row: indexPath.row, section: indexPath.section)
    }
    // MARK: 组头组尾
    func heightForHeaderInSection(section: Int) -> CGFloat {
        heightForHeader
    }
    func heightForFooterInSection(section: Int) -> CGFloat {
        heightForFooter
    }
    // 组头 子类重写
    func headerViewForSection(section: Int, contentView:UIView)  { }
    // 组尾 子类重写
    func footerViewForSection(section: Int, contentView:UIView)  { }
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        heightForHeaderInSection(section: section)
    }
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        heightForFooterInSection(section: section)
    }
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let headerView = XXView.init(frame: kFrame(0, 0, CGFloat(kScreenW), heightForHeader), color: .background)
        headerViewForSection(section: section, contentView: headerView)
        return headerView
    }
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        let footerView = XXView.init(frame: kFrame(0, 0, kScreenW, heightForFooter), color: .background)
        footerViewForSection(section: section, contentView: footerView)
        return footerView
    }
    // MARK: delegte
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if let block = clickBlock{
            if isCanClick{
                block(indexPath.row,indexPath.section)
                isCanClick = false
                XXGCD.delay(selectTimeInterval) { [weak self] in
                        self?.isCanClick = true
                }
            }
        }
    }
}

// MARK: 示例代码
/*
 
 class TestTableView: XXBaseTableView {

     override func config() {
         rows = 10
         heightForRow = 100
         cellClass = TestCell.self
         clickBlock = {row,section in
             
         }
         
         sections = 2
     }
     
     override func heightForRow(row: Int, section: Int) -> CGFloat {
         if row == 0{
             return 100
         }
         return 50
     }
     
     override func contentView<T>(forRow row: Int, section: Int, contentView: UIView, cell: T) where T : UITableViewCell {
         let cell = cell as! TestCell
         cell.setTitle(row.string)
     }
     
     override func heightForHeaderInSection(section: Int) -> CGFloat {
         if section == 0{
             return 40
         }
         return 10
     }
     
     override func heightForFooterInSection(section: Int) -> CGFloat {
         if section == 0{
             return 10
         }
         return 40
     }
     
     override func headerViewForSection(section: Int, contentView: UIView) {
         contentView.backgroundColor = .random
     }
     
     override func footerViewForSection(section: Int, contentView: UIView) {
         contentView.backgroundColor = .random
     }
     
 }

 
 */
